package com.itheiam.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheiam.domain.User;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/demo")
public class DemosServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //字符串数据
        String str = "ok";
        List<User> list = new ArrayList<User>();
        User user = new User();
        user.setUsername("a");
        user.setPwd("123");
        User user1 = new User();
        user1.setUsername("b");
        user1.setPwd("321");

        list.add(user);
        list.add(user1);
        //将json对象转换成json格式字符串
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(list);
        //响应页面数据
        resp.getWriter().write(json);
    }
}
